/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_goods_class.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.tb_discharge_cargo_address.entity.TbDischargeCargoAddress;
import com.thinkgem.jeesite.modules.tb_goods_class.entity.TbGoodsClass;
import com.thinkgem.jeesite.modules.tb_goods_class.service.TbGoodsClassService;

/**
 * 货物类型管理Controller
 * @author ZhangYangZe
 * @version 2017-05-13
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_goods_class/tbGoodsClass")
public class TbGoodsClassController extends BaseController {

	@Autowired
	private TbGoodsClassService tbGoodsClassService;
	
	@ModelAttribute
	public TbGoodsClass get(@RequestParam(required=false) String id) {
		TbGoodsClass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbGoodsClassService.get(id);
		}
		if (entity == null){
			entity = new TbGoodsClass();
		}
		return entity;
	}
	
	@RequiresPermissions("tb_goods_class:tbGoodsClass:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbGoodsClass tbGoodsClass, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbGoodsClass> page = tbGoodsClassService.findPage(new Page<TbGoodsClass>(request, response), tbGoodsClass); 
		model.addAttribute("page", page);
		return "modules/tb_goods_class/tbGoodsClassList";
	}

	@RequiresPermissions("tb_goods_class:tbGoodsClass:view")
	@RequestMapping(value = "form")
	public String form(TbGoodsClass tbGoodsClass, Model model) {
		model.addAttribute("tbGoodsClass", tbGoodsClass);
		return "modules/tb_goods_class/tbGoodsClassForm";
	}

	@RequiresPermissions("tb_goods_class:tbGoodsClass:edit")
	@RequestMapping(value = "save")
	public String save(TbGoodsClass tbGoodsClass, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbGoodsClass)){
			return form(tbGoodsClass, model);
		}
		tbGoodsClassService.save(tbGoodsClass);
		addMessage(redirectAttributes, "保存货物类型成功");
		return "redirect:"+Global.getAdminPath()+"/tb_goods_class/tbGoodsClass/?repage";
	}
	
	@RequiresPermissions("tb_goods_class:tbGoodsClass:edit")
	@RequestMapping(value = "delete")
	public String delete(TbGoodsClass tbGoodsClass, RedirectAttributes redirectAttributes) {
		tbGoodsClassService.delete(tbGoodsClass);
		addMessage(redirectAttributes, "删除货物类型成功");
		return "redirect:"+Global.getAdminPath()+"/tb_goods_class/tbGoodsClass/?repage";
	}
	@RequestMapping("CargoDetails")
	@ResponseBody
	public ArrayList<TbGoodsClass> CargoDetails(TbGoodsClass tbGoodsClass, HttpServletRequest request, HttpServletResponse response, Model model) {
		String id = request.getParameter("id");
		ArrayList<TbGoodsClass> CargoDetails = tbGoodsClassService.CargoDetails(id);
		return CargoDetails;
	}
}